package com.ebc.eservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ebc.eservice.mapper.MenuMapper;
import com.ebc.eservice.mapper.MenuRoleMapper;
import com.ebc.eservice.mapper.RoleMapper;
import com.ebc.eservice.pojo.EbcMenu;
import com.ebc.eservice.pojo.EbcRole;
import com.ebc.eservice.pojo.MenuRole;
import com.ebc.eservice.pojo.dto.MenuItem;
import com.ebc.eservice.pojo.dto.Route;
import com.ebc.eservice.service.EbcRoleService;
import com.ebc.eservice.util.CurrentUser;
import com.ebc.eservice.util.JsonResult;
import com.ebc.eservice.vo.Role.ModifyRoleRespVo;
import com.ebc.eservice.vo.Role.addRoleReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Joey
 * @Description: 角色-权限 业务
 * @date:2022/10/4 23:14
 */
@Service
public class RoleImp implements EbcRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MenuRoleMapper menuRoleMapper;

    //        QueryWrapper<EbcRole> wrapper = new QueryWrapper<>();
    //        wrapper.eq("open",1);//role_id小于等于2的权限
    @Override
    public JsonResult getRoleList() {//获取所有角色列表
        List<EbcRole> roles = roleMapper.selectList(null);
        return JsonResult.success(roles);
    }

    //获取所有权限菜单
    @Override
    public JsonResult getMenuList() {
        QueryWrapper<EbcMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id",17);
        List<EbcMenu> menuList = menuMapper.selectList(queryWrapper);
        List<Route> routeMenu = getRouteMenu(menuList);
        return JsonResult.success(routeMenu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addNewRole(addRoleReqVo reqVo) { //roleName roleCode
        EbcRole role = new EbcRole();
        CurrentUser currentUser = new CurrentUser();
        role.setCreateId(currentUser.getCurrentEbcUser().getId()).setCreator(currentUser.getCurrentEbcUser()
                .getName()).setDel(0).setRoleName(reqVo.getRoleName()).setRoleCode(reqVo.getRoleCode());
        int updateById = roleMapper.insert(role);

        if(updateById>0){

            EbcRole ebcRole = roleMapper.selectById(role.getId());
            //添加权限
            List<Integer> roleMenuIdList = reqVo.getRoleMenuIdList();// menuId

//            List<Integer> MenuOneZeroList = new ArrayList<>();
            if(roleMenuIdList.size() != 0){
                MenuRole menuRole=null;
                for (int i = 1; i < 20; i++) {
                    if(roleMenuIdList.contains(i)){
                        menuRole =new MenuRole();
                        menuRole.setMenuId(i); //菜单id
                        menuRole.setRoleId(ebcRole.getId());
                        menuRole.setOpen(1);
                    }else{
                        menuRole =new MenuRole();
                        menuRole.setMenuId(i); //菜单id
                        menuRole.setRoleId(ebcRole.getId());
                        menuRole.setOpen(0);
                    }
                    menuRoleMapper.insert(menuRole);
                }
            }else{
                return JsonResult.fail("添加权限为空,角色已创建");
            }

        }else{
            return JsonResult.fail("添加失败,获取异常");
        }
        return JsonResult.success("添加成功");
    }



    //修改菜单权限 role_id  menu_id  树状图 选中为1 未选中为0  只传选中的  每次全部更新
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult ModifyMenuRole(ModifyRoleRespVo respVo) {

        //修改角色信息
        EbcRole ebcRole = roleMapper.selectById(respVo.getRoleId());
        ebcRole.setRoleName(respVo.getRoleName());
        roleMapper.updateById(ebcRole);

        //修改菜单权限
        QueryWrapper<MenuRole> wrapper = null;

        // 菜单id  选中的菜单
        List<Integer> roleMenuIdList = respVo.getRoleMenuIdList();
        if(roleMenuIdList.size() != 0){
            MenuRole menuRole=null;
            for (int i = 1; i < 20; i++) {
                if(roleMenuIdList.contains(i)){
                    wrapper = new QueryWrapper<>();
                    wrapper.eq("role_id",respVo.getRoleId());
                    wrapper.eq("menu_id",i);
                    menuRole = menuRoleMapper.selectOne(wrapper);
                    menuRole.setOpen(1);
                    menuRoleMapper.updateById(menuRole);
                }else{
                    wrapper = new QueryWrapper<>();
                    wrapper.eq("role_id",respVo.getRoleId());
                    wrapper.eq("menu_id",i);
                    menuRole = menuRoleMapper.selectOne(wrapper);
                    menuRole.setOpen(0);
                    menuRoleMapper.updateById(menuRole);
                }
            }
        }

        return JsonResult.success("修改成功");
    }

    public List<Route> getRouteMenu(List<EbcMenu> menuList) {
        List<Route> routeList = new ArrayList<>();
        for (int i = 1; i < 6; i++) {
            MenuItem menuItem;
            List<MenuItem> menuItems;
            for (EbcMenu e : menuList) {
                if(e.getId() == i){
                    Route route = new Route();
                    route.setId(e.getId());
                    route.setTitle(e.getName());
                    route.setIndex(e.getPath());
                    route.setIcon(e.getIcon());
                    menuItems = new ArrayList<>();
                    for (EbcMenu item : menuList){
                        if(item.getPid() == i){
                            menuItem = new MenuItem();
                            menuItem.setItemId(item.getId());
                            menuItem.setTitle(item.getName());
                            menuItem.setIndex(item.getPath());
//                            menuItem.setComponent(item.getComponent());
                            menuItems.add(menuItem);
                        }
                    }
                    route.setMenuItems(menuItems);
                    routeList.add(route);
                }
            }
        }
        return routeList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult DeleteRole(ModifyRoleRespVo pVo) {
        roleMapper.deleteById(pVo.getRoleId());

        return JsonResult.success("删除成功 roleId: "+pVo.getRoleId());
    }
}
